<template>
	<view class="yonghufankui">
		<view class="box" v-for="(tab,i) in data.goodsList" :key="i">
			<view class="shangpin">
				<image :src="tab.picUrl" mode="aspectFill"></image>
				<view class="shangpin-box">
					<text class="shangpin-box-text">{{tab.goodsName}}</text>
					<text class="shangpin-box-text1">{{tab.specifications[0]}}</text>
				</view>
			</view>
			<view class="box-item">
				<view :class="tab.tabIndex===index?'active-box-item-box' : 'box-item-box'" v-for="(item,index) in list" :key="index" @click="handlehaopingdu(i,index)">
					<image v-if="tab.tabIndex != index && item.star == 1" class="box-item-box-image" src="../../static/pj_hp_1@2x.png" mode=""></image>
					<image v-if="tab.tabIndex == index && item.star == 1" class="box-item-box-image" src="../../static/pj_hp_2@2x.png" mode=""></image>
					<text v-if="item.star == 1" :class="tab.tabIndex===index?'active-box-item-box-text' : 'box-item-box-text'">好评</text>
					
					<image v-if="tab.tabIndex != index && item.star == 2" class="box-item-box-image" src="../../static/pj_zp_1@2x.png" mode=""></image>
					<image v-if="tab.tabIndex == index && item.star == 2" class="box-item-box-image" src="../../static/pj_zp_2@2x.png" mode=""></image>
					<text v-if="item.star == 2" :class="tab.tabIndex===index?'active-box-item-box-text' : 'box-item-box-text'">中评</text>
					
					<image v-if="tab.tabIndex != index && item.star == 3" class="box-item-box-image" src="../../static/pj_cp_1@2x.png" mode=""></image>
					<image v-if="tab.tabIndex == index && item.star == 3" class="box-item-box-image" src="../../static/pj_cp_2@2x.png" mode=""></image>
					<text v-if="item.star == 3" :class="tab.tabIndex===index?'active-box-item-box-text' : 'box-item-box-text'">差评</text>
				</view>
			</view>
			<view class="box-content">
				<textarea v-model="tab.content" placeholder="输入评价" placeholder-color="#999999"></textarea>
			</view>
			<!-- 上传图片 -->
			<view class="releaseGoodsItemUpload">
			  <view v-for="(item,index) in tab.photoList" :key="index" class="photoListItem">
				<image class="photoListItemImg" :src="item" @click="ViewImage(tab.photoList[index],tab.photoList)"></image>
				<view class="delImage" @click="DelImg(i,index)">
				    <image src="http://juebai.oss-cn-hangzhou.aliyuncs.com/webStorage/web_16147431265761867.png"></image>
				</view>
			  </view>
			  <image class="upload" @click="handleUpload(i)" v-if="photoList.length < 9" src="../../static/sc_tp@2x.png" mode=""></image>
			</view>
			
		</view>
		<view class="box-btn">
			<view class="btn" @click="$u.throttle(handleshenqing, 1000)">
				提交评价
			</view>
		</view>
	</view>
</template>

<script>
	var uploadImage = require('../../common/uploadFile.js');//地址换成你自己存放文件的位置
	var util = require('../../common/formatTime.js');
	export default {
		data() {
			return {
				list:[{
					star:1
				},{
					star:2
				},{
					star:3
				}],
				listIndex:0,
				content:'',
				photoList:[],
				data:'',
				orderId:'',
				share:uni.getStorageSync('share')
			}
		},
		onLoad(options) {
			this.orderId = options.orderId
			this.getorderDetails()
		},
		methods: {
			//订单详情
			async getorderDetails(){
				const params = {
					orderId:this.orderId
				}
				const res = await this.$u.api.orderDetails(params)
				this.data = res.data
				this.data.goodsList.forEach(v=>{
					v.tabIndex = 0
					v.star = 1
					v.photoList = []
					v.content = ''
				})
			},
			//上传图片
			  handleUpload(index){
			    var that = this;
			    uni.chooseImage({
			      count: 9, // 默认最多一次选择9张图
			      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			      //sourceType: ['album'],
			      success: function (res) {
			        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
			        var tempFilePaths = res.tempFilePaths;
			        var nowTime = util.formatTime(new Date());
			        //支持多图上传
			        console.log(res.tempFilePaths,12)
			        for (var i = 0; i < res.tempFilePaths.length; i++) {
			          //显示消息提示框
			          uni.showLoading({
			            title: '上传中' + (i + 1) + '/' + res.tempFilePaths.length,
			            mask: true
			          })
			          //上传图片
			          //你的域名下的/cbb文件下的/当前年月日文件下的/图片.png
			          //图片路径可自行修改
			          // uploadImage(res.tempFilePaths[i], 'bad/' + nowTime + '/',
			          uploadImage(res.tempFilePaths[i], 'loveFruit/',
			            function (result) {
			              console.log("======上传成功图片地址为：", result);
						  console.log(index,'iiiii')
			              //做你具体的业务逻辑操作
						  let data = that.data.goodsList[index]
						  data.photoList = data.photoList.concat(result.split(','))
						  that.data.goodsList.splice(index,1,data)
						  console.log(that.data.goodsList)
			              uni.hideLoading();
			           }, function (result) {
			            console.log("======上传失败======", result);
			            //做你具体的业务逻辑操作
			                   uni.hideLoading()
			                }
			             )
			          }
			       }
			    })
			},
			ViewImage(url,photoList) {
				console.log(url,photoList)
			    uni.previewImage({
			        urls: photoList,
			        current: url
			    });
			},
			DelImg(i,index) {
				let data = this.data.goodsList[i]
				data.photoList.splice(index,1)
				this.data.goodsList.splice(i,1,data)
			},
			//选择
			handlehaopingdu(i,index1){
				let data = this.data.goodsList[i]
				data.tabIndex = index1
				this.data.goodsList.splice(i,1,data)
				this.data.goodsList[i].star = this.list[index1].star
			},
			//提交评价
			async handleshenqing(){
				var commentList = []
				this.data.goodsList.forEach(v=>{
					if(v.content != '' || v.photoList.length != 0){
						commentList.push({
							goodsId:v.goodsId,
							content:v.content,
							star:v.star,
							img:v.photoList
						})
					}
				})
				const params = {
					orderId:this.orderId,
					comment:commentList
				}
				const res = await this.$u.api.comment(params)
				this.$u.toast(res.msg)
				uni.navigateBack({
					delta:arguments
				})
			}
		}
	}
</script>
<style>
	page{
		background: #FBFBFB;
	}
</style>
<style scoped lang="scss">
	.yonghufankui{
		padding-top: 28rpx;
		padding-bottom: 100rpx;
	}
	.box{
		padding: 28rpx;
		background: white;
	}
	.shangpin{
		padding: 16rpx;
		background: #F4F4F4;
		display: flex;
		flex-direction: row;
	}
	.shangpin image{
		width: 116rpx;
		height: 116rpx;
	}
	.shangpin-box{
		margin-left: 16rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		
	}
	.shangpin-box-text{
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
		line-height: 28rpx;
	}
	.shangpin-box-text1{
		font-size: 24rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
		line-height: 28rpx;
	}
	.box-item{
		padding: 28rpx 0rpx;
        border-top: 0.5px solid #E3E3E3;
		border-bottom: 0.5px solid #E3E3E3;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		margin: 28rpx 0;
	}
	.box-item-box{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: 216rpx;
		height: 84rpx;
		background: #F4F4F4;
	}
	.active-box-item-box{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: 216rpx;
		height: 84rpx;
		background: url('http://hmh-app.oss-cn-beijing.aliyuncs.com/webStorage/web_16919793742137642.png') no-repeat;
		background-size: 100% 100%;
	}
	.box-item-box-image{
		width: 44rpx;
		height: 44rpx;
	}
	.active-box-item-box-text{
		margin-left: 16rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 32rpx;
	}
	.box-item-box-text{
		margin-left: 16rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
		line-height: 32rpx;
	}
	.box-content{
		
	}
	.box-content textarea{
		width: 100%;
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #999999;
		line-height: 32rpx;
	}
	
	.releaseGoodsItemUpload{
	  display: flex;
	  flex-direction: row;
	  flex-wrap: wrap;
	  padding: 50rpx 0rpx;
	}
	.photoListItem{
	  width: 140rpx;
	  height: 140rpx;
	  position: relative;
	  margin: 0rpx 20rpx 20rpx 0rpx;
	}
	.photoListItemImg{
	  width: 140rpx;
	  height: 140rpx;
	  border-radius: 10px;
	}
	.delImage image{
	  width: 40rpx;
	  height: 40rpx;
	  position: absolute;
	  top: 0;
	  right: 0;
	}
	.upload{
	  margin-bottom: 20rpx;
	  width: 140rpx;
	  height: 140rpx;
	}
	
    .box-btn{
    	padding: 10rpx 28rpx 0rpx 28rpx;
    	width: 750rpx;
    	height: 168rpx;
    	background: #FFFFFF;
        border-top: 0.5px solid #E3E3E3;
    	position: fixed;
    	left: 0;
    	bottom: 0;
    }
    .btn{
    	width: 694rpx;
    	height: 84rpx;
    	background: url('http://hmh-app.oss-cn-beijing.aliyuncs.com/webStorage/web_16919793742137642.png') no-repeat;
    	background-size: 100% 100%;
    	font-size: 32rpx;
    	font-family: PingFang SC-Bold, PingFang SC;
    	font-weight: bold;
    	color: #FFFFFF;
    	line-height: 84rpx;
    	text-align: center;
    }
</style>
